import unittest
from parameterized import parameterized

import page
from base import log
from page.page_app_login import PageAppLogin
from page.page_app_order import PageAppOrder
from util import GetDriver, read_json, write_json


class TestAppLoginOrder(unittest.TestCase):
    def setUp(self) -> None:
        self.driver = GetDriver.get_app_driver()
        # 获取PageAppLogin实例
        self.login = PageAppLogin(self.driver)
        # 获取PageAppOrder实例
        self.order = PageAppOrder(self.driver)

    def tearDown(self) -> None:
        self.driver.quit()

    @parameterized.expand(read_json("app_order.json","order"))
    def test01_login_order(self, search_name, pwd, expect_text):
        try:
            # 调用登录
            self.login.page_app_login()
            # 打印昵称断言
            nickname = self.login.page_app_get_nickname()
            print("登录账户昵称：", nickname)
            self.assertEqual(expect_text,nickname)

            # 下订单
            self.order.page_app_order(search_name, pwd)
            # 打印订单号断言
            order_on = self.order.page_app_get_order_number()
            print("订单号：", order_on)
            # 记录订单编号 --》 发货完成后做断言使用
            write_json(order_on[5:])
            log.info("===========>app下单成功，订单编号为：{}".format(read_json("expect.json", "expect")[0][0]))
        except Exception as e:
            log.error(e)
            # 截图
            self.order.base_get_img()
            raise
